<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"
               >
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handleAdd(scope.row,scope.index)"
          v-if="permissions.sys_menu_add"
        >新增子项
        </el-button>
        <el-button
          size="small"
          type="text"
          icon="el-icon-circle-plus-outline"
          @click="handleDataRule(scope.row)"
          v-if="permissions.sys_menu_perm">数据规则
        </el-button>
      </template>
      <template slot-scope="scope" slot="icon">
        <i :class="scope.row.icon"/>
      </template>
    </avue-crud>

    <el-drawer
      :title="title"
      :size="size"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <data-rule v-if="drawer" ref="dataRule"></data-rule>
    </el-drawer>

  </basic-container>
</template>

<script>
  import {addObj,getObj, putObj, delObj, fetchMenuTree} from '@/api/admin/menu'
  import DataRule from  '../datarule/data-rule'
  import {option} from '@/const/crud/admin/menu'
  import {mapGetters} from "vuex";

  export default {
    components: {DataRule},
    data() {
      return {
        size: '50%',
        title: '数据权限配置',
        drawer: false,
        direction: 'rtl',
        form: {},
        loading: true,
        selectionList: [],
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: option,
        data: [],
        treedata: []
      };
    },

    computed: {
      ...mapGetters(["userInfo", "permissions"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions['sys_menu_add'], false),
          delBtn: this.vaildData(this.permissions['sys_menu_del'], false),
          editBtn: this.vaildData(this.permissions['sys_menu_edit'], false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleAdd(row) {
        this.$refs.crud.value.parentId = row.id;
        this.$refs.crud.option.column.filter(item => {
          if (item.prop === "parentId") {
            item.value = row.id;
            item.addDisabled = true;
          }
        });
        this.$refs.crud.rowAdd();
      },
      rowSave(row, done, loading) {
        if (row.parentId === undefined || row.parentId === "") {
          row.parentId = -1
        }
        addObj(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        putObj(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return delObj(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getObj(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        fetchMenuTree(Object.assign(params, this.query)).then(res => {
          this.loading = false;
          this.data = res.data.data;
          const column = this.findObject(this.option.column, "parentId");
          column.dicData = res.data.data;
        });
      },

      handleDataRule(row) {
        this.title = '['+ row.name+']'+' '+'数据权限配置';
        this.drawer = true;
        this.$nextTick(() => {
          this.$refs.dataRule.init(row.id)
        })
      },
      handleClose(done) {
        done();
      },
      treeLoad(tree, treeNode, resolve) {

        this.query.parentId = tree.id;
        fetchMenuTree(this.query).then(res => {
          this.treedata = res.data.data;
          resolve(
            this.treedata
          )
        });

      }

    }
  };
</script>

<style>
</style>
